package org.shoukaiseki.common.domain.excel;

import org.shoukaiseki.common.exception.ImpExcelException;
import org.shoukaiseki.common.utils.StringUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.StringJoiner;

/**
 *
 **/
public class ImpExcelMessage {

    private int rowCount;

    private int currentIndex = -1;

    private int successNum = 0;

    private int failureNum = 0;

    private int ignoreNum = 0;

    private List<ImpExcelMessageRow> successMsg= new ArrayList();

    //失败的消息
    private List<ImpExcelMessageRow> failureMsg= new ArrayList();

    //忽略的消息
    private List<ImpExcelMessageRow> ignoreMsg= new ArrayList();

    private String message;

    public ImpExcelMessage(String message){
        this.message = message;
    }

    public ImpExcelMessage(int rowCount){
        this.rowCount=rowCount;
        this.init();
    }

    private void init(){
    }

    public boolean next(){
        this.currentIndex++;
        if(currentIndex >=rowCount){
            return false;
        }
        return true;
    }

    //下一个索引位
    public int nextIndex(){
        return currentIndex;
    }

    public int getExcelRowNum(){
        return this.currentIndex +2;
    }

    public void addSuccessMessage(String msg){
        successNum++;
        successMsg.add(new ImpExcelMessageRow()
                .setNum(currentIndex)
                .setMessage(msg)
        );
    }


    public void addFailureMessage(String msg){
        failureNum++;
        failureMsg.add(new ImpExcelMessageRow()
                .setNum(currentIndex)
                .setMessage(msg)
        );
    }

    public void addIgnoreMsg(String msg) {
        ignoreNum++;
        ignoreMsg.add(new ImpExcelMessageRow()
                .setNum(currentIndex)
                .setMessage(msg)
        );
    }

    public ImpExcelMessage formatMessageSuccessAndIgnore(){
        StringJoiner sj=new StringJoiner("<br>");
        if (!ignoreMsg.isEmpty()) {
            sj.add(StringUtils.format("部分忽略导入！共 {} 条数据,忽略的数据如下：",ignoreNum));
            for (ImpExcelMessageRow row : ignoreMsg) {
                sj.add(StringUtils.format("{} - {}",String.format("%1$-"+String.valueOf(currentIndex).length()+"s",""+row.getNum()),row.getMessage()));
            }
        }

        if (!successMsg.isEmpty()) {
            sj.add(StringUtils.format("导入成功共 {} 条数据,数据如下：" , successNum));
            for (ImpExcelMessageRow row : successMsg) {
                sj.add(StringUtils.format("{} - {}",String.format("%1$-"+String.valueOf(currentIndex).length()+"s",""+row.getNum()),row.getMessage()));
            }
        }

        this.message=sj.toString();
        return this;
    }

    public void hasFailureMsgThrow(){
        StringJoiner sj=new StringJoiner("<br>");
        if (!this.failureMsg.isEmpty()) {
            sj.add(StringUtils.format("导入校验错误 {} 条数据,数据如下：" , failureNum));
            for (ImpExcelMessageRow row : failureMsg) {
                sj.add(StringUtils.format("{} - {}",String.format("%1$-"+String.valueOf(currentIndex).length()+"s",""+row.getNum()),row.getMessage()));
            }
            throw new ImpExcelException(sj.toString()).setHtmlMsg(true);
        }
    }

    public String getMessage() {
        return message;
    }
}
